import fs from "fs"
import {fileURLToPath} from "url"
import {controller} from "../config/appStting.js";

export let getControllerUrl = function () {
    let controllerFile = new URL(controller, import.meta.url).href
    let controllerUrl = fileURLToPath(controllerFile)
    let url = fs.readdirSync(controllerFile).filter(item => item.endsWith("controller")).map(item => `${item}/${controllerFile}`)
    return url
}
export let regController = function (router, controllerFiles) {
    controllerFiles.forEach(
        async file => {
            let {default: obj} = await import(file)
            obj.forEach(
                obj => {
                    if (obj.method === "get") {
                        router.get(obj.url, obj.url)
                    }
                    if (obj.method === "post") {
                        router.post(obj.url, obj.url)
                    }
                    if (obj.method === "put") {
                        router.put(obj.url, obj.url)
                    }
                    if (obj.method === "del") {
                        router.delete(obj.url, obj.url)
                    }


                }
            )
        }
    )
}